// 精灵系统 - 提供更好的游戏素材
class Sprite {
    constructor(width, height) {
        this.width = width;
        this.height = height;
        this.canvas = document.createElement('canvas');
        this.canvas.width = width;
        this.canvas.height = height;
        this.ctx = this.canvas.getContext('2d');
    }
    
    // 获取精灵图像
    getImage() {
        return this.canvas;
    }
}

// 玩家精灵
class PlayerSprite extends Sprite {
    constructor() {
        super(32, 32);
        this.drawPlayer();
    }
    
    drawPlayer() {
        const ctx = this.ctx;
        
        // 身体
        ctx.fillStyle = '#4A90E2';
        ctx.fillRect(8, 12, 16, 16);
        
        // 头部
        ctx.fillStyle = '#FFD700';
        ctx.fillRect(12, 6, 8, 8);
        
        // 眼睛
        ctx.fillStyle = '#000000';
        ctx.fillRect(14, 8, 2, 2);
        ctx.fillRect(16, 8, 2, 2);
        
        // 武器
        ctx.fillStyle = '#8B4513';
        ctx.fillRect(20, 14, 6, 2);
        
        // 边框
        ctx.strokeStyle = '#FFFFFF';
        ctx.lineWidth = 1;
        ctx.strokeRect(8, 6, 16, 22);
    }
}

// 敌人精灵
class EnemySprite extends Sprite {
    constructor(type) {
        super(24, 24);
        this.type = type;
        this.drawEnemy();
    }
    
    drawEnemy() {
        const ctx = this.ctx;
        
        switch(this.type) {
            case ENEMY_TYPES.ZOMBIE:
                this.drawZombie(ctx);
                break;
            case ENEMY_TYPES.SKELETON:
                this.drawSkeleton(ctx);
                break;
            case ENEMY_TYPES.GHOST:
                this.drawGhost(ctx);
                break;
            case ENEMY_TYPES.BOSS:
                this.drawBoss(ctx);
                break;
        }
    }
    
    drawZombie(ctx) {
        // 身体
        ctx.fillStyle = '#228B22';
        ctx.fillRect(6, 10, 12, 12);
        
        // 头部
        ctx.fillStyle = '#8B4513';
        ctx.fillRect(8, 4, 8, 8);
        
        // 眼睛
        ctx.fillStyle = '#FF0000';
        ctx.fillRect(10, 6, 2, 2);
        ctx.fillRect(12, 6, 2, 2);
        
        // 边框
        ctx.strokeStyle = '#FFFFFF';
        ctx.lineWidth = 1;
        ctx.strokeRect(6, 4, 12, 18);
    }
    
    drawSkeleton(ctx) {
        // 身体
        ctx.fillStyle = '#F5F5DC';
        ctx.fillRect(6, 10, 12, 12);
        
        // 头部
        ctx.fillStyle = '#FFFFFF';
        ctx.fillRect(8, 4, 8, 8);
        
        // 眼睛
        ctx.fillStyle = '#000000';
        ctx.fillRect(10, 6, 2, 2);
        ctx.fillRect(12, 6, 2, 2);
        
        // 边框
        ctx.strokeStyle = '#000000';
        ctx.lineWidth = 1;
        ctx.strokeRect(6, 4, 12, 18);
    }
    
    drawGhost(ctx) {
        // 身体
        ctx.fillStyle = '#E6E6FA';
        ctx.fillRect(6, 10, 12, 12);
        
        // 头部
        ctx.fillStyle = '#F0F8FF';
        ctx.fillRect(8, 4, 8, 8);
        
        // 眼睛
        ctx.fillStyle = '#4B0082';
        ctx.fillRect(10, 6, 2, 2);
        ctx.fillRect(12, 6, 2, 2);
        
        // 边框
        ctx.strokeStyle = '#FFFFFF';
        ctx.lineWidth = 1;
        ctx.strokeRect(6, 4, 12, 18);
    }
    
    drawBoss(ctx) {
        // 身体
        ctx.fillStyle = '#8B0000';
        ctx.fillRect(4, 8, 16, 16);
        
        // 头部
        ctx.fillStyle = '#DC143C';
        ctx.fillRect(6, 2, 12, 12);
        
        // 眼睛
        ctx.fillStyle = '#FFD700';
        ctx.fillRect(8, 4, 3, 3);
        ctx.fillRect(13, 4, 3, 3);
        
        // 边框
        ctx.strokeStyle = '#FF0000';
        ctx.lineWidth = 2;
        ctx.strokeRect(4, 2, 16, 22);
    }
}

// 武器精灵
class WeaponSprite extends Sprite {
    constructor(type) {
        super(16, 16);
        this.type = type;
        this.drawWeapon();
    }
    
    drawWeapon() {
        const ctx = this.ctx;
        
        switch(this.type) {
            case WEAPON_TYPES.KNIFE:
                this.drawKnife(ctx);
                break;
            case WEAPON_TYPES.FIREBALL:
                this.drawFireball(ctx);
                break;
            case WEAPON_TYPES.LIGHTNING:
                this.drawLightning(ctx);
                break;
            case WEAPON_TYPES.ICE_SHARD:
                this.drawIceShard(ctx);
                break;
            case WEAPON_TYPES.LASER:
                this.drawLaser(ctx);
                break;
            case WEAPON_TYPES.ORBITAL:
                this.drawOrbital(ctx);
                break;
            case WEAPON_TYPES.CLONE:
                this.drawClone(ctx);
                break;
            case WEAPON_TYPES.SHIELD:
                this.drawShield(ctx);
                break;
        }
    }
    
    drawKnife(ctx) {
        // 刀身
        ctx.fillStyle = '#C0C0C0';
        ctx.fillRect(6, 4, 8, 2);
        ctx.fillRect(12, 6, 2, 4);
        
        // 刀柄
        ctx.fillStyle = '#8B4513';
        ctx.fillRect(4, 6, 4, 4);
        
        // 边框
        ctx.strokeStyle = '#FFFFFF';
        ctx.lineWidth = 1;
        ctx.strokeRect(4, 4, 10, 8);
    }
    
    drawFireball(ctx) {
        // 火焰
        ctx.fillStyle = '#FF4500';
        ctx.fillRect(6, 6, 4, 4);
        ctx.fillStyle = '#FF8C00';
        ctx.fillRect(8, 4, 2, 8);
        ctx.fillRect(4, 8, 8, 2);
        
        // 边框
        ctx.strokeStyle = '#FF0000';
        ctx.lineWidth = 1;
        ctx.strokeRect(4, 4, 8, 8);
    }
    
    drawLightning(ctx) {
        // 闪电
        ctx.fillStyle = '#00FFFF';
        ctx.fillRect(6, 2, 2, 4);
        ctx.fillRect(8, 6, 2, 4);
        ctx.fillRect(10, 10, 2, 4);
        
        // 边框
        ctx.strokeStyle = '#FFFFFF';
        ctx.lineWidth = 1;
        ctx.strokeRect(6, 2, 6, 12);
    }
    
    drawIceShard(ctx) {
        // 冰锥
        ctx.fillStyle = '#87CEEB';
        ctx.fillRect(8, 2, 2, 8);
        ctx.fillRect(6, 10, 6, 2);
        
        // 边框
        ctx.strokeStyle = '#FFFFFF';
        ctx.lineWidth = 1;
        ctx.strokeRect(6, 2, 6, 10);
    }
    
    drawLaser(ctx) {
        // 激光
        ctx.fillStyle = '#FF1493';
        ctx.fillRect(6, 6, 4, 4);
        ctx.fillRect(8, 4, 2, 8);
        
        // 边框
        ctx.strokeStyle = '#FF69B4';
        ctx.lineWidth = 1;
        ctx.strokeRect(6, 4, 4, 8);
    }
    
    drawOrbital(ctx) {
        // 环绕弹
        ctx.fillStyle = '#32CD32';
        ctx.fillRect(6, 6, 4, 4);
        
        // 旋转效果
        ctx.strokeStyle = '#00FF00';
        ctx.lineWidth = 1;
        ctx.beginPath();
        ctx.arc(8, 8, 6, 0, Math.PI * 2);
        ctx.stroke();
    }
    
    drawClone(ctx) {
        // 分身
        ctx.fillStyle = '#9370DB';
        ctx.fillRect(6, 6, 4, 4);
        
        // 分身效果
        ctx.strokeStyle = '#8A2BE2';
        ctx.lineWidth = 1;
        ctx.strokeRect(4, 4, 8, 8);
        ctx.strokeRect(6, 6, 4, 4);
    }
    
    drawShield(ctx) {
        // 护盾
        ctx.fillStyle = '#FFD700';
        ctx.fillRect(6, 6, 4, 4);
        
        // 护盾效果
        ctx.strokeStyle = '#FFFF00';
        ctx.lineWidth = 2;
        ctx.beginPath();
        ctx.arc(8, 8, 6, 0, Math.PI * 2);
        ctx.stroke();
    }
}

// 道具精灵
class ItemSprite extends Sprite {
    constructor(type) {
        super(20, 20);
        this.type = type;
        this.drawItem();
    }
    
    drawItem() {
        const ctx = this.ctx;
        
        switch(this.type) {
            case 'experience':
                this.drawExperience(ctx);
                break;
            case 'health':
                this.drawHealth(ctx);
                break;
            case 'shield':
                this.drawShield(ctx);
                break;
            case 'speed':
                this.drawSpeed(ctx);
                break;
            case 'minion':
                this.drawMinion(ctx);
                break;
        }
    }
    
    drawExperience(ctx) {
        // 经验宝石
        ctx.fillStyle = '#FFD700';
        ctx.fillRect(6, 6, 8, 8);
        
        // 闪光效果
        ctx.fillStyle = '#FFFF00';
        ctx.fillRect(8, 8, 4, 4);
        
        // 边框
        ctx.strokeStyle = '#FFA500';
        ctx.lineWidth = 1;
        ctx.strokeRect(6, 6, 8, 8);
    }
    
    drawHealth(ctx) {
        // 生命道具
        ctx.fillStyle = '#FF6B6B';
        ctx.fillRect(8, 4, 4, 8);
        ctx.fillRect(6, 6, 8, 4);
        
        // 边框
        ctx.strokeStyle = '#FF0000';
        ctx.lineWidth = 1;
        ctx.strokeRect(6, 4, 8, 8);
    }
    
    drawShield(ctx) {
        // 护盾道具
        ctx.fillStyle = '#4ECDC4';
        ctx.fillRect(6, 6, 8, 8);
        
        // 护盾效果
        ctx.strokeStyle = '#00FFFF';
        ctx.lineWidth = 2;
        ctx.beginPath();
        ctx.arc(10, 10, 6, 0, Math.PI * 2);
        ctx.stroke();
    }
    
    drawSpeed(ctx) {
        // 速度道具
        ctx.fillStyle = '#45B7D1';
        ctx.fillRect(6, 6, 8, 8);
        
        // 速度效果
        ctx.strokeStyle = '#00FFFF';
        ctx.lineWidth = 1;
        ctx.beginPath();
        ctx.moveTo(6, 10);
        ctx.lineTo(14, 10);
        ctx.moveTo(10, 6);
        ctx.lineTo(10, 14);
        ctx.stroke();
    }
    
    drawMinion(ctx) {
        // 小兵道具
        ctx.fillStyle = '#32CD32';
        ctx.fillRect(6, 6, 8, 8);
        
        // 小兵标识
        ctx.fillStyle = '#FFFFFF';
        ctx.fillRect(8, 8, 4, 4);
        
        // 边框
        ctx.strokeStyle = '#00FF00';
        ctx.lineWidth = 1;
        ctx.strokeRect(6, 6, 8, 8);
    }
}

// 分身精灵
class CloneSprite extends Sprite {
    constructor() {
        super(28, 28);
        this.drawClone();
    }
    
    drawClone() {
        const ctx = this.ctx;
        
        // 身体
        ctx.fillStyle = '#9370DB';
        ctx.fillRect(8, 12, 12, 12);
        
        // 头部
        ctx.fillStyle = '#8A2BE2';
        ctx.fillRect(10, 6, 8, 8);
        
        // 眼睛
        ctx.fillStyle = '#FFFFFF';
        ctx.fillRect(12, 8, 2, 2);
        ctx.fillRect(14, 8, 2, 2);
        
        // 边框
        ctx.strokeStyle = '#8A2BE2';
        ctx.lineWidth = 1;
        ctx.strokeRect(8, 6, 12, 18);
    }
}

// 小兵精灵
class MinionSprite extends Sprite {
    constructor() {
        super(20, 20);
        this.drawMinion();
    }
    
    drawMinion() {
        const ctx = this.ctx;
        
        // 身体
        ctx.fillStyle = '#32CD32';
        ctx.fillRect(6, 10, 8, 8);
        
        // 头部
        ctx.fillStyle = '#228B22';
        ctx.fillRect(8, 4, 4, 8);
        
        // 眼睛
        ctx.fillStyle = '#FFFFFF';
        ctx.fillRect(9, 6, 1, 1);
        ctx.fillRect(10, 6, 1, 1);
        
        // 边框
        ctx.strokeStyle = '#00FF00';
        ctx.lineWidth = 1;
        ctx.strokeRect(6, 4, 8, 14);
    }
}

// 精灵管理器
class SpriteManager {
    constructor() {
        this.sprites = new Map();
        this.initSprites();
    }
    
    initSprites() {
        // 玩家精灵
        this.sprites.set('player', new PlayerSprite());
        
        // 敌人精灵
        this.sprites.set('zombie', new EnemySprite(ENEMY_TYPES.ZOMBIE));
        this.sprites.set('skeleton', new EnemySprite(ENEMY_TYPES.SKELETON));
        this.sprites.set('ghost', new EnemySprite(ENEMY_TYPES.GHOST));
        this.sprites.set('boss', new EnemySprite(ENEMY_TYPES.BOSS));
        
        // 武器精灵
        this.sprites.set('knife', new WeaponSprite(WEAPON_TYPES.KNIFE));
        this.sprites.set('fireball', new WeaponSprite(WEAPON_TYPES.FIREBALL));
        this.sprites.set('lightning', new WeaponSprite(WEAPON_TYPES.LIGHTNING));
        this.sprites.set('ice_shard', new WeaponSprite(WEAPON_TYPES.ICE_SHARD));
        this.sprites.set('laser', new WeaponSprite(WEAPON_TYPES.LASER));
        this.sprites.set('orbital', new WeaponSprite(WEAPON_TYPES.ORBITAL));
        this.sprites.set('clone', new WeaponSprite(WEAPON_TYPES.CLONE));
        this.sprites.set('shield', new WeaponSprite(WEAPON_TYPES.SHIELD));
        
        // 道具精灵
        this.sprites.set('experience', new ItemSprite('experience'));
        this.sprites.set('health', new ItemSprite('health'));
        this.sprites.set('shield_item', new ItemSprite('shield'));
        this.sprites.set('speed', new ItemSprite('speed'));
        this.sprites.set('minion', new ItemSprite('minion'));
        
        // 分身和小兵精灵
        this.sprites.set('clone_entity', new CloneSprite());
        this.sprites.set('minion_entity', new MinionSprite());
    }
    
    getSprite(name) {
        return this.sprites.get(name);
    }
    
    drawSprite(ctx, name, x, y, width, height) {
        const sprite = this.getSprite(name);
        if (sprite) {
            ctx.drawImage(sprite.getImage(), x - width/2, y - height/2, width, height);
        }
    }
} 